import { updateBoxVolumeApi, updateBoxVolumePushTtApi } from "@/api/waybillManagement/customerServiceWaybill/index";
import { Ref } from "vue";
import { message } from "ant-design-vue";

export default function OperationHook(state: { loading: Ref<boolean>; emit: any; tableData: Ref<Array<CustomerServiceWaybill.ListRows>>; type: Ref<number> }) {
  const { emit, loading, tableData, type } = state;

  const submitHandle = () => {
    let requestFn;
    const parmas = tableData.value
      .filter((t: any) => t.shipmentBoxNumber)
      .map((t: any) => ({
        pickingBoxLength: t.pickingBoxLength,
        pickingBoxWidth: t.pickingBoxWidth,
        pickingBoxHeight: t.pickingBoxHeight,
        pickingBoxWeight: t.pickingBoxWeight,
        id: t.id
      }));

    if (type.value === 1) {
      requestFn = updateBoxVolumeApi;
    } else {
      requestFn = updateBoxVolumePushTtApi;
    }

    loading.value = true;
    requestFn(parmas)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功");
          emit("submit");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const resetHandle = () => {
    tableData.value = [];
    emit("update:visible", false);
  };

  return {
    submitHandle,
    resetHandle
  };
}
